<template>
  <view class="page_diy_edit page_new_student_registration_edit" :style="{ paddingTop: vuex_custom_bar_height + 'px' }">
    <tn-nav-bar>新生报到</tn-nav-bar>
    <view class="page-custom " id="page_diy_edit">
      <view class="custom-from">
          <view v-if="$check_field('set', 'student_name') || $check_field('add', 'student_name') ||  $check_field('get', 'student_name')" class="custom-from-item">
          <view class="label">
            <text>
              学生姓名
            </text>
          </view>
              <!-- 文本 -->
                  <view class="content">
            <input type="text" id="form_student_name" v-model="form['student_name']" placeholder="请输入学生姓名" v-if="(form['student_name'] && $check_field('set','student_name')) || (!form['student_name'] && $check_field('add','student_name'))" :disabled="disabledObj['student_name_isDisabled']" />
            <text v-else-if="$check_field('get','student_name')">{{ form['student_name'] }}</text>
          </view>
                    </view>
            <view v-if="$check_field('set', 'student_id') || $check_field('add', 'student_id') ||  $check_field('get', 'student_id')" class="custom-from-item">
          <view class="label">
            <text>
              学生学号
            </text>
          </view>
              <!-- 文本 -->
                  <view class="content">
            <input type="text" id="form_student_id" v-model="form['student_id']" placeholder="请输入学生学号" v-if="(form['student_id'] && $check_field('set','student_id')) || (!form['student_id'] && $check_field('add','student_id'))" :disabled="disabledObj['student_id_isDisabled']" />
            <text v-else-if="$check_field('get','student_id')">{{ form['student_id'] }}</text>
          </view>
                    </view>
            <view v-if="$check_field('set', 'student_account') || $check_field('add', 'student_account') ||  $check_field('get', 'student_account')" class="custom-from-item">
          <view class="label">
            <text>学生账户</text>
          </view>
          <view class="content">
            <uni-data-select
                    id="form_student_account" v-model="form['student_account']"
                    :localdata="list_user_student_account"
                    :clear="!disabledObj['student_account_isDisabled']"
                    :disabled="disabledObj['student_account_isDisabled']"
                    v-if="(form['student_account'] && $check_field('set', 'student_account')) || (!form['student_account'] && $check_field('add', 'student_account'))" style="width: 100%;">
            </uni-data-select>
            <text v-else-if="$check_field('get', 'student_account')">
              {{form['student_account']}}
            </text>
          </view>
        </view>
            <view v-if="$check_field('set', 'registration_time') || $check_field('add', 'registration_time') ||  $check_field('get', 'registration_time')" class="custom-from-item">
          <view class="label">
            <text>
              报到时间
            </text>
          </view>
              <!-- 日长 -->
          <view class="content">
            <uni-datetime-picker :disabled="disabledObj['registration_time_isDisabled']" type="datetime" id="form_registration_time"
                                 v-model="form['registration_time']" placeholder="请输入保质时间" v-if="
                    (form['registration_time'] && $check_field('set', 'registration_time')) ||
                    (!form['registration_time'] && $check_field('add', 'registration_time'))
                    " />
            <text v-else-if="$check_field('get', 'registration_time')">{{ form['registration_time'] }}</text>
          </view>
            </view>
            <view v-if="$check_field('set', 'class_name') || $check_field('add', 'class_name') ||  $check_field('get', 'class_name')" class="custom-from-item">
          <view class="label">
            <text>
              班级名称
            </text>
          </view>
              <!-- 选项 -->
          <view class="content">
            <uni-data-select
                    id="form_class_name"
                    v-model="form['class_name']"
                    :localdata="list_class_name"
                    v-if="(form['class_name'] && $check_field('set','class_name')) || (!form['class_name'] && $check_field('add','class_name'))"
				    style="width: 100%;"
                                ></uni-data-select>
            <text v-else-if="$check_field('get','class_name')">{{ form['class_name'] }}</text>
          </view>
            </view>
            <view v-if="$check_field('set', 'registration_remarks') || $check_field('add', 'registration_remarks') ||  $check_field('get', 'registration_remarks')" class="custom-from-item">
          <view class="label">
            <text>
              报到备注
            </text>
          </view>
              <!-- 多文本 -->
          <view class="content">
            <textarea id="form_registration_remarks" v-model="form['registration_remarks']" v-if="(form['registration_remarks'] && $check_field('set','registration_remarks')) || (!form['registration_remarks'] && $check_field('add','registration_remarks'))" :disabled="disabledObj['registration_remarks_isDisabled']"/>
            <text v-else-if="$check_field('get','registration_remarks')">{{ form['registration_remarks'] }}</text>
          </view>
            </view>
          <view v-if="user_group === '管理员' || $check_examine()" class="custom-from-item">
          <view class="label">
            <text> 审核状态 </text>
          </view>
          <view class="content ">
            <uni-data-select v-model="form['examine_state']" :localdata="list_examine_state"></uni-data-select>
            <text style="margin-left: 10rpx;">
              {{ form['examine_state'] }}
            </text>
          </view>
        </view>
        <view v-if="user_group === '管理员' || $check_examine()" class="custom-from-item">
          <view class="label">
            <text>审核回复 </text>
          </view>
          <view class="content">
            <textarea v-model="form['examine_reply']"> </textarea>
          </view>
        </view>
        <view class="form-footer">
          <view class="me-btn" @click="submit()">提交</view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import mixin from '@/libs/mixins/page.js';
export default {
  mixins: [mixin],
  data() {
    return {
      url_get_obj: '~/api/new_student_registration/get_obj?',
      url_add: '~/api/new_student_registration/add?',
      url_set: '~/api/new_student_registration/set?',
	  today: new Date().toISOString().split('T')[0],
      // 查询条件
      query: {
        new_student_registration_id: 0,
          student_name: "",
            student_id: "",
            student_account: 0,
            registration_time: "",
            class_name: "",
            registration_remarks: "",
        },
      obj: {
        new_student_registration_id: 0,
          student_name:  '', // 学生姓名
            student_id:  '', // 学生学号
            student_account: 0, // 学生账户
            registration_time: this.$toTime(new Date().getTime(), "yyyy-MM-dd hh:mm:ss"),
            class_name:  '', // 班级名称
            registration_remarks:  '', // 报到备注
          examine_state: "未审核",
        examine_reply: "",
      },

      // 表单字段
      form: {
        new_student_registration_id: 0,
          student_name:  '', // 学生姓名
            student_id:  '', // 学生学号
            student_account: 0, // 学生账户
            registration_time: this.$toTime(new Date().getTime(), "yyyy-MM-dd hh:mm:ss"),
            class_name:  '', // 班级名称
            registration_remarks:  '', // 报到备注
          examine_state: "未审核",
        examine_reply: "",
      },
      disabledObj: {
          student_name_isDisabled: false,
            student_id_isDisabled: false,
            student_account_isDisabled: false,
            registration_time_isDisabled: false,
            class_name_isDisabled: false,
            registration_remarks_isDisabled: false,
        },
                              // 用户列表
      list_user_student_account: [],
                              list_class_name: [],
                      field: 'new_student_registration_id',
      table_key: 'new_student_registration',

      list_examine_state: [
        { value: '未审核', text: '未审核' },
        { value: '已通过', text: '已通过' },
        { value: '未通过', text: '未通过' },
      ],
    };
  },
  methods: {
    /**
     * 提交前验证事件
     * @param {Object} 请求参数
     * @return {String} 验证成功返回null, 失败返回错误提示
     */
    async submit_check(param) {
                                                          if (!param.registration_time){
        return "报到时间不能为空";
      }
                                                      return null;
    },


    
                
                
        /**
    * 获取学生用户用户列表
    */
    async get_list_user_student_account() {
        var json = await this.$get("~/api/user/get_list?user_group=学生用户");
        if(json.result && json.result.list){
          json.result.list.map((o) => this.list_user_student_account.push({value:o.user_id,text:o.nickname + '-' + o.username}));
        }
        else if(json.error){
          console.error(json.error);
        }
    },
            async get_user_session_student_account(){
        var _this = this;
        var json = await this.$get("~/api/user_group/get_obj?name=学生用户");
        if(json.result && json.result.obj){
          var source_table = json.result.obj.source_table;
          var user_id = _this.userInfo.user_id;
          if (user_id){
            var url = "~/api/"+source_table+"/get_obj"
            this.$get(url, { user_id: _this.userInfo.user_id }, function (res) {
              if (res.result && res.result.obj) {
                var arr = []
                for (let key in res.result.obj) {
                  arr.push(key)
                }
                var arrForm = []
                for (let key in _this.form) {
                  arrForm.push(key)
                }
                _this.form["student_account"] = user_id
                _this.disabledObj['student_account' + '_isDisabled'] = true
                for (var i=0;i<arr.length;i++){
                  if (arr[i]!=='examine_state' && arr[i]!=='examine_reply') {
                    for (var j = 0; j < arrForm.length; j++) {
                      if (arr[i] === arrForm[j]) {
                        if (arr[i] !== "student_account") {
                          _this.form[arrForm[j]] = res.result.obj[arr[i]]
                          _this.disabledObj[arrForm[j] + '_isDisabled'] = true
                          break;
                        }
                      }
                    }
                  }
                }
              }
            });
          }
        }
        else if(json.error){
          console.error(json.error);
        }
    },
            
                    /**
    * 获取班级名称列表
    */
    async get_list_class_name() {
                    var json = await this.$get("~/api/class_information/get_list?");
        if(json.result && json.result.list){
          json.result.list.map((o) => this.list_class_name.push({value:o.class_name,text:o.class_name}));
        }
        else if(json.error){
          console.error(json.error);
        }
          },
                    
                
                change_img(key_name) {
      var _self = this;
      _self.upload_img_flag = false;
      // 选择图像方法
      uni.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album'], //从相册选择
        success: function (res) {
          const tempFilePaths = res.tempFilePaths;
          const uploadTask = uni.uploadFile({
            url: _self.$fullUrl('~/api/new_student_registration/upload?'),
            filePath: tempFilePaths[0],
            name: 'file',
            formData: {
              new_student_registration: 'test',
            },
            header: {
              'x-auth-token': _self.$store.state.app.userInfo.token,
            },
            success: function (uploadFileRes) {
              var filename = JSON.parse(uploadFileRes.data).result.url;
              var img_url = filename;
              _self.form[key_name] = img_url;
            },
          });

          uploadTask.onProgressUpdate(function (res) {
            _self.percent = res.progress;
            console.log('上传进度' + res.progress);
            console.log('已经上传的数据长度' + res.totalBytesSent);
            console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
          });
        },
        error: function (e) {
          console.log(e);
        },
      });
    },

    /**
     * 获取对象后获取缓存表单
     * @param {Object} json
     * @param {Object} func
     */
    get_obj_before(param) {
      var form = uni.db.get('form');
      if (form) {
        delete form.examine_state;
        delete form.examine_reply;
        this.obj = uni.push(this.obj, form);
        this.form = uni.push(this.form, form);
      }
      var arr = [];
      for (let key in form) {
        arr.push(key);
      }
      for (var i = 0; i < arr.length; i++) {
        this.disabledObj[arr[i] + '_isDisabled'] = true;
      }
                      if (this.form["registration_time"] && JSON.stringify(this.form["registration_time"]).indexOf("-")===-1) {
        this.form["registration_time"] = this.$toTime(parseInt(this.form["registration_time"]), "yyyy-MM-dd hh:mm:ss")
      }
                  uni.db.del('form');
      return param;
    },

    /**
     * 获取对象后获取缓存表单
     * @param {Object} json
     * @param {Object} func
     */
    get_obj_after(json, func) {
      var form = uni.db.get('form');
      var obj = Object.assign({}, form, this.obj);
      if (form) {
        this.obj = uni.push(this.obj, obj);
      }
      if (form) {
        this.form = uni.push(this.form, form);
      }
      if (func) {
        func(json);
      }
    },

  },
  onLoad() {
                    this.get_user_session_student_account();
        this.get_list_user_student_account();
              this.get_list_class_name();
              },
};
</script>

